package cn.snowsoft.iot.module.warning.service.warning.action;

import cn.snowsoft.iot.framework.common.pojo.CommonResult;
import cn.snowsoft.iot.module.warning.controller.admin.home.vo.ActionTypeCountVO;
import cn.snowsoft.iot.module.warning.controller.admin.warning.action.vo.WarningActionPageVO;
import cn.snowsoft.iot.module.warning.controller.admin.warning.action.vo.WarningActionSaveOrUpdateVO;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.action.HTTPDO;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.action.MQTTDO;
import cn.snowsoft.iot.module.warning.dal.dataobject.warning.action.WarningActionDO;
import cn.snowsoft.iot.module.warning.dal.mysql.warning.action.WarningActionMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class WarningActionServiceImpl extends ServiceImpl<WarningActionMapper, WarningActionDO>  implements IWarningActionService {


    @Override
    public CommonResult saveUpdate(WarningActionSaveOrUpdateVO action) {
        WarningActionDO warningActionDO = new WarningActionDO();
        BeanUtils.copyProperties(action, warningActionDO);

        if ("3".equals(warningActionDO.getActionType())) {
            JSONObject jsonObject = new JSONObject();
            if (warningActionDO.getOutputWay().equals("MQTT")) {
                MQTTDO mqtt = action.getMqtt();
                jsonObject.put("mqttAddress", mqtt.getMqttAddress());
                jsonObject.put("clsId", mqtt.getClsId());
                jsonObject.put("userName", mqtt.getUserName());
                jsonObject.put("passWord", mqtt.getPassWord());
                jsonObject.put("qos", mqtt.getQos());
                jsonObject.put("retain", mqtt.getRetain());
                jsonObject.put("theme", mqtt.getTheme());
                String jsonString = jsonObject.toJSONString();
                warningActionDO.setRelatedParameter(jsonString);
                saveOrUpdate(warningActionDO);
                return CommonResult.success(action);
            }
            if (warningActionDO.getOutputWay().equals("HTTP")) {
                HTTPDO http = action.getHttp();
                jsonObject.put("requestPath", http.getRequestPath());
                jsonObject.put("requestMethod", http.getRequestMethod());
                String jsonString = jsonObject.toJSONString();
                warningActionDO.setRelatedParameter(jsonString);
                saveOrUpdate(warningActionDO);
                return CommonResult.success(action);
            }
        }

        if("1".equals(warningActionDO.getActionType())){
            saveOrUpdate(warningActionDO);
            return CommonResult.success(warningActionDO);
        }
        return null;
    }

    @Override
    public IPage<WarningActionDO> selectWarningPage(WarningActionPageVO actionPage) {
        IPage<WarningActionDO> page = new Page<>(actionPage.getPageNo(), actionPage.getPageSize());
        return baseMapper.selectActionPage(page, actionPage);
    }

    @Override
    public Boolean removeAction(List<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    @Override
    public CommonResult getActionById(Long id) {

        WarningActionDO actionDO = getById(id);
        WarningActionSaveOrUpdateVO warningAction = new WarningActionSaveOrUpdateVO();
        if ("3".equals(actionDO.getActionType())) {
            ObjectMapper mapper = new ObjectMapper();
            if (actionDO.getOutputWay().equals("MQTT")) {
                try {
                    MQTTDO mqttdo = mapper.readValue(actionDO.getRelatedParameter(), MQTTDO.class);
                    BeanUtils.copyProperties(actionDO, warningAction);
                    warningAction.setMqtt(mqttdo);
                    return CommonResult.success(warningAction);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
            if (actionDO.getOutputWay().equals("HTTP")) {
                try {
                    HTTPDO httpdo = mapper.readValue(actionDO.getRelatedParameter(), HTTPDO.class);
                    BeanUtils.copyProperties(actionDO, warningAction);
                    warningAction.setHttp(httpdo);
                    return CommonResult.success(warningAction);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if ("1".equals(actionDO.getActionType())){

            BeanUtils.copyProperties(actionDO, warningAction);
            if (StringUtils.isNotEmpty(actionDO.getRelatedParameter())){
                JSONObject jsonObject = JSONObject.parseObject(actionDO.getRelatedParameter());
                warningAction.setMessage(jsonObject);
            }

        }

        return CommonResult.success(warningAction);
    }

    @Override
    public List<ActionTypeCountVO> getActionTypeCount() {
        return baseMapper.getActionTypeCount();
    }

    @Override
    public List<WarningActionDO> getByIds(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        return baseMapper.selectList(new LambdaQueryWrapper<WarningActionDO>().in(WarningActionDO::getId, list));
    }
}
